"""
@project: PyCharm
@author: MZM
@file: database.py
@ide: PyCharm
@time: 2021/7/24 16:46
@desc： 
"""
import pymysql

host = 'localhost'
port = 3306
db = 'shopxo'
user = 'root'
password = ''

class Database(object):
    def __init__(self,commit=True):
        self._commit = commit

    def __enter__(self):
        self._conn = pymysql.connect(host = host,port = port,db = db,user = user,password = password)
        self._cursor = self._conn.cursor(pymysql.cursors.DictCursor)
        self._conn.autocommit = False
        return self

    def __exit__(self, *exc_info):
        if self._commit:
            self._conn.commit()
        self._cursor.close()
        self._conn.close()

    def fetch_one(self,sql,param):
        self._cursor.execute(sql,param)
        return self._cursor.fetchone()